package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /*
    添加购物车
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();
        //id存入购物车
        shoppingCart.setUserId(currentId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //获取套餐id
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,currentId);
        if(dishId!=null){
            //菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            //套餐
            qw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one!=null){
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }else{
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(currentId);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }



    /*
    查询购物车
     */
    @GetMapping("/list")
    public R list() {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }
    /*
    取消购物车
     */
    @PostMapping("/sub")
    public R cancel(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            //有菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            //有套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number>0){
            shoppingCartService.updateById(one);
        }
        if (number<=0) {
            shoppingCartService.removeById(one);
        }
        return R.success(one);
    }
    /*
    清空购物车
     */
    @DeleteMapping("/clean")
    public R clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(qw);
        return R.success("购物车清空成功");
    }


}